<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/27 9:43
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\apiadmin\controller;


use app\common\model\admin\FilesModel;


use app\common\utils\Res;
use think\facade\Filesystem;
class Uploads extends AuthController
{


    public function index(){
        $filesModel = new FilesModel();
        $files = $this->request->file('file');
        if(empty($files)){
            return  Res::error(201,'上传文件错误');
        }
        $extension = $files->getOriginalExtension();
        $getSaveName=str_replace("\\","/",$files->getOriginalName());
        $img = "bmp,jpg,jpeg,png,tiff,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf";
        if(strpos($img,$extension)!=false){
            $type=0;
        }
        $txt = "txt, doc, docx, xls, htm, html, jsp, rtf, wpd, pdf, ppt ";
        if(strpos($txt,$extension)!=false){
            $type=1;
        }
        $video=" mp4, avi, mov, wmv, asf, navi, 3gp, mkv, f4v, rmvb, webm";
        if(strpos($video,$extension)!=false){
            $type=2;
        }
        $savename = Filesystem::disk('public')->putFile( $extension, $files);
        $sname = str_replace("\\","/",$savename);
        $data = ['name' => $sname,
            'ftype' => $extension,
            'type'=>$type,
            'urls' =>'/storage/'.$sname,
            'creat_time' =>date('Y-m-d H:i:s',time()),
            'oss'=>'1',
            'creat_by'=>$this->request->username
        ];
        $filesModel->add($data);
        return Res::ok("上传成功",$sname);
    }


}